from django.db import models
from apps.oaauth.models import OAAbstractUser,OADepartment

class Inform(models.Model):
    title = models.CharField(max_length=100,verbose_name='标题')
    content = models.TextField(verbose_name='内容')
    create_time = models.DateTimeField(auto_now_add=True,verbose_name='创建时间')
    #如果前端上传的department中包含0，如[0]，则认为这个通知是所有部门可见的
    public = models.BooleanField(default=False,verbose_name='是否公开')
    author = models.ForeignKey(OAAbstractUser,on_delete=models.CASCADE, related_name='informs',related_query_name='informs')
    # 序列化的时候用，前端传部门id,我们通过department_id获取部门对象
    department = models.ManyToManyField(OADepartment,related_name='informs',related_query_name='informs')
    class  Meta:
        ordering = ['-create_time']

# 是否已读
class InformRead(models.Model):
    inform = models.ForeignKey(Inform,on_delete=models.CASCADE, related_name='reads',related_query_name='reads')
    user = models.ForeignKey(OAAbstractUser,on_delete=models.CASCADE, related_name='reads',related_query_name='reads')
    read_time = models.DateTimeField(auto_now_add=True)
    class Meta:
        # inform和user组合的数据是唯一的
        unique_together = ('inform','user')
